var STip = ZFT.Util.STip;
var ParseTemplate = ZFT.Util.ParseTemplate;
var Template = {
    baseinfo: ParseTemplate(require("../tpl/baseform.xtpl")),
    edittelmodal: ParseTemplate(require("../tpl/baseinfomodal.xtpl"))
}
var BaseInfo = ZFT.Util.Class({
    container: $("body"),
    timer: null,
    RESEND_VCODE_TIME: 120,
    __canLoad: true,
    init: function () {

    },
    EVENTS: {
        "click #baseInfo": "loadBaseInfo",
        "click #saveNewAddrBtn": "saveNewAddr",
        "click #modifyPwd": "modifyPwd",
       // "click #modifyTelBtn": "showModifyTelModal",

    },
    loadBaseInfo: function (e) {
        var tar = (e) ? $(e.currentTarget) : $("#baseInfo");
        var _this = this;
        if (tar.hasClass("active") && _this.__canLoad == false) return false;
        tar.addClass("active").siblings().removeClass("active");
        ZFT.Util.Ajax("/r/Member_getSelfInfo", {
            type: "POST",
            dataType: "json",
            success: function (res) {
                if (res.code == 200) {
                    var html = Template.baseinfo({ data: res.data });
                    $("#centerContent").html(html);
                    _this.__canLoad = false;
                } else {
                    return STip("fail", res.msg);
                }

            }
        })


    },
    saveNewAddr: function () {
        var newAddr = $("#newAddr").val();
        var newAddrBtn = $("#saveNewAddrBtn");
        if (!newAddr) return STip("fail", "请输入联系地址！", 4000);
        ZFT.Util.Ajax("/r/Member_updateAddress", {
            type: "POST",
            dataType: "json",
            params: {
                address: newAddr
            },
            success: function (res) {
                if (res.code == 200) {
                    STip("success", res.msg, 4000);
                } else {
                    STip("fail", res.msg, 4000);
                }
            }
        })
    },
    modifyPwd: function () {
        var pwdBox = $("#pwdBox");
        pwdBox.show(200);
        $("#savePwdBtn").on("click", function () {
            var params = {
                old_pwd: $("#oldPwd").val(),
                new_pwd: $("#newPwd").val(),
                confirm_pwd: $("#confirmPwd").val()
            }
            if (!params["old_pwd"]) return STip("fail", "请填写平台密码", 4000);
            if (!params["new_pwd"]) return STip("fail", "请填写新密码", 4000);
            if (!params["confirm_pwd"]) return STip("fail", "请填写确认新密码", 4000);
            ZFT.Util.Ajax("/r/Member_updatePassword", {
                type: "POST",
                dataType: "json",
                params: params,
                success: function (res) {
                    if (res.code == 200) {
                        STip("success", res.msg, 4000);
                        pwdBox.hide(200).find(".u-form_control").val("");
                    } else {
                        STip("fail", res.msg, 4000);
                    }
                }
            })
        })
    },
    showModifyTelModal: function (e) {
        var _this = this;
        var $tel = $(e.currentTarget).attr("data-tel");
        var editModal = Template.edittelmodal({ tel: $tel });
        var modal = $("#modal");
        var regTel = /^1\d{10}$/;
        var regCode = /^\d{6}$/;
        modal.html(editModal);
       /* $("#sendTelCodeBtn").on("click", function (e) {
            var tarBtn = $(e.currentTarget);
            var last_time = _this.RESEND_VCODE_TIME;
            var mobile = $("#mobileInp").val();
            var oldTel = tarBtn.attr("data-tel");
            params = {
                mobile: mobile,
                type: (oldTel == mobile) ? 0 : 1
            }

            ZFT.Util.Ajax("/r/VerifyCode_sendChangeMobileCode", {
                type: "POST",
                dataType: "json",
                params: params,
                success: function (res) {
                    if (res.code == 200) {
                        STip("success", res.msg, 4000);
                        clearInterval(_this.timer);
                        tarBtn.removeClass("m-btn_ol_default").text(last_time + "秒后重发");
                        _this.timer = setInterval(function () {
                            if (last_time == 0) {
                                tarBtn.addClass("m-btn_ol_default").text("获取验证码");
                                return clearInterval(_this.timer);
                            }
                            last_time--;
                            tarBtn.removeClass("m-btn_ol_default");
                            tarBtn.text(last_time + "秒后重发")
                        }, 1000)
                    } else {
                        clearInterval(_this.timer);
                        tarBtn.addClass("m-btn_ol_default").text("获取验证码");
                        return STip("fail", res.msg, 4000);
                    }
                }
            })

        })*/
        //保存新手机号
        /*$("#saveBtn").on("click", function (e) {
            var tarBtn = $(e.currentTarget);
            var mobile = $("#mobileInp").val();
            var code = $("#codeInp").val();
            var oldTel = $("#sendTelCodeBtn").attr("data-tel");
            var newMobile = $("#newMobileInp").val();
            var params = {
                mobile: mobile,
                mobile_code: code,
                type: (mobile == oldTel) ? 0 : 1
            }
            //console.log(params)
            if (!regCode.test(code)) return STip("fail", "请输入6位数字验证码");
            if (!regTel.test(mobile)) return STip("fail", "请输入11位手机号！");
            if (!regTel.test(newMobile)) return STip("fail", "请输入11位新手机号");
            //校验手机号和短信验证码
            ZFT.Util.Ajax("/r/VerifyCode_checkChangeMobile", {
                type: "POST",
                dataType: "json",
                params: params,
                success: function (res) {
                    if (res.code == 200) {
                        var param = {
                            old_mobile: oldTel,
                            new_mobile: newMobile
                        }
                        ZFT.Util.Ajax("/r/Member_changeMobile", {
                            type: "POST",
                            dataType: "json",
                            params: param,
                            success: function (res) {
                                if (res.code == 200) {
                                    STip("success", res.msg, 2000);
                                    modal.html("");
                                    _this.__canLoad = true;
                                    _this.loadBaseInfo();
                                } else {
                                    clearInterval(_this.timer);
                                    $("#sendTelCodeBtn").addClass("m-btn_ol_default").text("获取验证码");
                                    return STip("fail", res.msg);

                                }

                            }
                        })
                    } else {
                        clearInterval(_this.timer);
                        $("#sendTelCodeBtn").addClass("m-btn_ol_default").text("获取验证码");
                        return STip("fail", res.msg, 4000);
                    }
                }
            })

        })*/
    },

})

$(function () {
    new BaseInfo();
})